/**
 * \file
 *           tlmRmtCfgParaAdmin.cpp
 * \brief
 *           Implements tlmRmtCfgParaAdmin class for Telematics services
 *           this is the class of parameters for admin.
 *
 * \par Author (last changes):
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Project Leader:
 *          -
 * \par Responsible Developer:
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Responsible Architect:
 *          - Zhang Fa
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 *
 * \par Project:
 *      DPCA2
 *
 * \par SW-Component:
 *      Telematics
 *
 * \par SW-Package:
 *      Telematics
 *
 * \par SW-Module:
 *      Telematics Application
 *
 * \par SW-Architecture Specification:
 *      DPCA2_TELEMATICS_SW_DD_07a_ready.doc
 *
* \par SW-Design Specification:
 *      DPCA2_TLM-RMTCFG_SW_DD_01d_released.doc
 *
 * \par SW-Test Case Specification:
 *
 * \par CM-Info:
 *
 * \note
 *      This is a note.
 *
 * \bug
 *      - there are no known bugs
 *
 * \par Module-History:
 *
 * \verbatim
 * Date                Author					Reason
 * 10 Dec 2013        Zeng Jie              Initial Creation
 * The latest entry at the head of the history list.
 * \endverbatim
 *
 * \par Copyright Notice:
 * Copyright (C) 2012 Continental Automotive Holdings Co., Ltd.
 * Alle Rechte vorbehalten. All Rights Reserved.\n
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.\n
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 *
 */

#include "stdafx.h"

#include "tlmServiceFactory.h"
#include "tlmRmtCfgParaAdmin.h"
#include "string.h"
#include "teleLogTrace.h"

#include "app_IEmbRegistryIDs.h"
#include "teleMainBL.h"
//#]


/**
* \brief tlmRmtCfgParaAdmin: this is constructor of tlmRmtCfgParaAdmin class
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgParaAdmin::tlmRmtCfgParaAdmin() :tlmRmtCfgPara(TLM_RMT_CFG_PARA_ADMIN_ELEMENT_COUNT), 
                            mAdminUrl(TLM_RMT_CFG_ELEMENT_CODE_ADMIN_URL,TLM_RMT_CFG_PARA_ITEM_URL_VALUE_SIZE),
                            mRecFilter(TLM_RMT_CFG_ELEMENT_CODE_ADMIN_REC_FILTER),
                            mSrvVin(TLM_RMT_CFG_ELEMENT_CODE_ADMIN_SERVICE_VIN,TLM_RMT_CFG_PARA_ITEM_VIN_VALUE_SIZE) {
    
    //#[ operation tlmRmtCfgParaAdmin()
    mEleParaList[0]=&mAdminUrl;
    mEleParaList[1]=&mSrvVin;
    mEleParaList[2]=&mRecFilter;
	mSrvId=TLM_RMT_CFG_SERVICE_ID_ADMIN;
    //#]
}

/**
* \brief ~tlmRmtCfgParaAdmin: this is destructor of tlmRmtCfgParaAdmin class
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgParaAdmin::~tlmRmtCfgParaAdmin() {
}

/**
* \brief GetItem : get the parameter element by it's code
*
* \note
*
* \param[in]
* - code : the code of parameters element
*
* \return
* - tlmRmtCfgElementPara*: pointer to parameter element
*
*/
tlmRmtCfgElementPara* tlmRmtCfgParaAdmin::GetItem(uint8_t code) {
    //#[ operation GetItem(uint8_t)
    tlmRmtCfgElementPara * pItem=NIL;
    switch(code)
    {
    	case TLM_RMT_CFG_ELEMENT_CODE_ADMIN_URL:  
        {
    		pItem = &mAdminUrl;
    		break;
        }
    	case TLM_RMT_CFG_ELEMENT_CODE_ADMIN_SERVICE_VIN :   
        {
            pItem = &mSrvVin;
    	 	break;  
        }
    	case TLM_RMT_CFG_ELEMENT_CODE_ADMIN_REC_FILTER :
        {
    		pItem = &mRecFilter;
    	 	break;     	
        }
    	default:
        {
            //LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgParaAdmin::%s> invalid para code[%d]\n", __FUNCTION__, code);
    		break;
        }
    }    
    
    return pItem;
    
    //#]
}

/**
* \brief Save: save admin parameters into NVM
*              If any element is update, the all admin parameters shall be saved.
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint8_t: save status
*
*/
uint8_t tlmRmtCfgParaAdmin::Save() {
    
    //#[ operation Save()
    BOOL isUpdated = FALSE; 
    S_TLM_REMOTE_CFG_ADMIN_DATA sAdminData;   
    uint8_t status=0;
     
    //any item is updated, the total web parameters shall be save into NVM. 
    isUpdated = mAdminUrl.IsUpdated() |
                mSrvVin.IsUpdated() |
                mRecFilter.IsUpdated(); 
                
    if(isUpdated)
    {//the parameters is udpated  
    	memset(&sAdminData, 0, sizeof(sAdminData));
    	sAdminData.recFilter = *(reinterpret_cast<uint8_t *>(mRecFilter.GetValuePtr())); 
    	memcpy(sAdminData.serviceVIN, mSrvVin.GetValuePtr(), mSrvVin.GetActualSize());
    	memcpy(sAdminData.adminURL, mAdminUrl.GetValuePtr(), mAdminUrl.GetActualSize());
    	    
        status = SaveParameters(ERG_DPCA_TLM_REMOTECFG_ADMIN,reinterpret_cast<uint8_t*>(&sAdminData),sizeof(sAdminData));

        
        teleMainBL::getInstance().Admin_DetectVINConflict(VIN_SERVER);
        LOG_PRINT(DPCA_TELE_MAIN_INFO,"[TLM_MSGSRV]:<%s> service vin is updated\n", __FUNCTION__);
    } 
    
    return status;
    //#]
}

/**
* \brief InitUrl: Initialize the admin url of the element.
*
* \note
*
* \param[in]
* uint8_t * url: pointer to the url string
*
* \return 
* - none
*
*/
void tlmRmtCfgParaAdmin::InitUrl(uint8_t * url) {
    if(url != NIL)
    {
        mAdminUrl.InitValue(url);
    }
    
}

/**
* \brief InitRecFilter: Initialize the admin rec filter of the element.
*
* \note
*
* \param[in]
* uint8_t recFilter: recFilter value
*
* \return 
* - none
*
*/
void tlmRmtCfgParaAdmin::InitRecFilter(uint8_t recFilter) {
    
    mRecFilter.InitValue(&recFilter);
}

/**
* \brief InitSrvVin: Initialize the admin service vin of the element.
*
* \note
*
* \param[in]
* uint8_t * srvVin: pointer to the service vin code
*
* \return 
* - none
*
*/
void tlmRmtCfgParaAdmin::InitSrvVin(uint8_t * srvVin) {
    if(srvVin != NIL)
    {
        mSrvVin.InitValue(srvVin);
    }
    
}

/**
* \brief GetVinCode: get the service vin code of admin.
*
* \note
*
* \param[in]
* - none
*
* \return 
* uint8_t * : pointer to the service vin code.
*
*/
uint8_t * tlmRmtCfgParaAdmin::GetVinCode(void) const{
    return mSrvVin.getMValuePtr();
}




